package com.rocky.project.action.model;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;

import com.rocky.project.action.base.BaseAction;
import com.rocky.project.model.family.Archive;
import com.rocky.project.model.family.Family;
import com.rocky.project.model.family.Person;
import com.rocky.project.util.BeanUtil;
import com.rocky.project.util.WebUtil;

/**
 * @author new
 *
 */
public class ArchiveAction extends BaseAction {
	private static final long serialVersionUID = -3510474656970657392L;	
	private Family family;
	private Archive archive;
	
	
	public Archive getArchive() {
		return archive;
	}
	public void setArchive(Archive archive) {
		this.archive = archive;
	}
	public Family getFamily() {
		return family;
	}
	public void setFamily(Family family) {
		this.family = family;
	}
	public String read() throws Exception {
		List<Object> paramsvalues = new LinkedList<Object>();
		//and m.latest=false and m.snapTime is not  null
		String queryHql = "SELECT m FROM Archive AS m WHERE m.deleted = false    ";		
		String keywordFilterValue = WebUtil.getParaValue("keywordFilterValue");	
		String year = WebUtil.getParaValue("year");	
		
		if(id!=null && !id.equals("") ){
			queryHql +="    and m.family.id = ?   ";			
			paramsvalues.add(Long.valueOf(id));			
		}		
		if(StringUtils.isNotBlank(keywordFilterValue)){
			queryHql +="    and (m.familycode like ? or m.householder.name like ?  or m.householder.cardnumber like ? ) ";
			paramsvalues.add("%"+keywordFilterValue+"%");
			paramsvalues.add("%"+keywordFilterValue+"%");	
			paramsvalues.add("%"+keywordFilterValue+"%");				
		}
		if(StringUtils.isNotBlank(year)){
			queryHql +="    and m.snapTime between ? and ?   ";	
			SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date begin = simpleDateFormat.parse(year+"-01-01 00:00:00");
			Date end = simpleDateFormat.parse(year+"-12-31 23:59:59");			
			paramsvalues.add(begin);	
			paramsvalues.add(end);
		}
		if(StringUtils.isNotBlank(this.suc)&&this.suc.equals("archivereadold")){			
			queryHql +="   and m.latest=false and m.snapTime is not  null   ";
		}		
		queryHql = queryHql + "    order by m.id desc";
		this.pageRecords = baseService.executeHqlWithParam(queryHql,paramsvalues.toArray(),getStart(), getPageSize());
		this.rowCount = baseService.getCountHqlWithParam(queryHql, paramsvalues.toArray());		
		this.ajaxResultMap.put("data", this.pageRecords);
		this.ajaxResultMap.put("rowCount", this.rowCount);		
		log.info("ArchiveAction:readOld.action->"+this.rowCount);	
		if(StringUtils.isNotBlank(this.suc)){			
			return this.suc;
		}
		return ajaxResult;
	}	

	public String readYear() throws Exception {
		List<Object> paramsvalues = new LinkedList<Object>();
		String queryHql = "SELECT distinct m.year FROM ArchivingConfig AS m WHERE m.deleted = false  and m.realTime is null  ";	
		queryHql = queryHql + "    order by m.year desc";
		this.pageRecords = baseService.executeHqlWithParam(queryHql,paramsvalues.toArray(),getStart(), getPageSize());
//		this.rowCount = baseService.getCountHqlWithParam(queryHql, paramsvalues.toArray());		
		this.ajaxResultMap.put("data", this.pageRecords);
		this.ajaxResultMap.put("rowCount", this.rowCount);		
		log.info("ArchiveAction:readYear.action->"+this.rowCount);		
		return ajaxResult;
	}	
	@Override
	public String readOne() throws Exception {
		archive = (Archive) baseService.get(Archive.class, Long.valueOf(id));
		return super.readOne();
	}
	@Override
	public String createProcess() throws Exception {		
		JSONObject obj = JSONObject.fromObject(this.jsondata);
		archive = (Archive) JSONObject.toBean( obj, Archive.class );		
		WebUtil.init4create(archive);
		baseService.create(archive);		
		return ajaxResult;
	}
	@Override
	public String updateProcess() throws Exception {
		JSONObject obj = JSONObject.fromObject(this.jsondata);
		
		String id = obj.getString("id");	
		
		String familyid = obj.getString("familyid");		
		String familymemo = obj.getString("familymemo");		
		String householderid = obj.getString("householderid");
		
		String address = obj.getString("address");
//		String householdername = obj.getString("householdername");
		String familycode = obj.getString("familycode");	
		
		obj.remove("familyid");
		obj.remove("familymemo");
		obj.remove("householderid");		
		obj.remove("householdername");
		obj.remove("familycode");
		obj.remove("address");		
		
		archive = (Archive) JSONObject.toBean( obj, Archive.class );	
		Archive archiveDB=(Archive) baseService.get(Archive.class, Long.valueOf(id));		

		Family objDb =(Family) baseService.get(Family.class, Long.valueOf(familyid));	
		objDb.setFamilycode(familycode);
		objDb.setAddress(address);
		objDb.setMemo(familymemo);
		if(StringUtils.isNotBlank(householderid)){			
			Person person =(Person) baseService.get(Person.class, Long.valueOf(householderid));	
			objDb.setHouseholder(person);
		}			
		WebUtil.init4modify(objDb);	
		baseService.update(objDb);
		BeanUtil.copyProperties(archive, archiveDB,BeanUtil.DEFAULT_EXCLUDES);		
//		archiveDB.setFamily(objDb);
//		archiveDB.setAreanum(archive.getAreanum());
//		archiveDB.setAreacard(archive.getAreacard());
//		archiveDB.setAutonum(archive.getAutonum());
//		archiveDB.setOwntype(archive.getOwntype());
//		archiveDB.setConstructionArea(archive.getConstructionArea());
//		archiveDB.setFoundationArea(archive.getFoundationArea());
//		archiveDB.setUseful(archive.getUseful());		
		baseService.update(archiveDB);
		return ajaxResult;
	}
	@Override
	public String deleteProcess() throws Exception {
		deleteProcess(Family.class);
		return super.deleteProcess();
	}
	@SuppressWarnings("unchecked")
	@Override
	public String updateLink() throws Exception {
		//TODO 要根据前台传过来的archiveid和familyid进行判断 如果没有archiveid则生成新的archive，有的话直接返回该archive
		this.pageRecords = baseService.executeHqlNoParam("select m  from  Archive m where  m.deleted = false and m.latest = true and m.family.id = "+Long.valueOf(id) + "  order by id desc", 0, null);
		for(Iterator<Archive> iter = this.pageRecords.iterator();iter.hasNext();){
			archive=iter.next();
			this.ajaxResultMap.put("data", archive);		
			return "archive";
		}
		family = (Family) baseService.get(Family.class, Long.valueOf(id));
		archive = new Archive();
		archive.setFamily(family);
		baseService.create(archive);
		this.ajaxResultMap.put("data", archive);		
		return "archive";
	}

}